//
//  MemberViewController.swift
//  AutoPrint
//
//  Created by David on 16/10/20.
//  Copyright © 2016年 fatcat. All rights reserved.
//  个人页面

import UIKit

class MemberViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate {

    override func preferredStatusBarStyle() -> UIStatusBarStyle {
        return .LightContent
    }
    
    var scrollView: UIScrollView!
    var tableView:UITableView!
    let TABLE_ITEMS = ["个人资料","我的文件","红包卡券","系统设置","帮助关于"]
    
    var avatar: UIButton!
    var userBtn: UIButton!
    
    var logined = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = Fatcat.THEME_COLOR
        navigationController?.navigationBar.hidden = true
        navigationController?.navigationBar.barStyle = .Black
        navigationController?.navigationBar.barTintColor = Fatcat.THEME_COLOR
        navigationItem.titleView?.tintColor = UIColor.whiteColor()
        
        // 状态栏加个背景
        let statusView = UIView(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: 20))
        statusView.backgroundColor = Fatcat.THEME_COLOR
        view.addSubview(statusView)
        
        
        // MARK: scrollView
        scrollView = UIScrollView(frame: CGRect(x: 0, y: 20, width: view.bounds.width, height: view.bounds.height - 20))
        scrollView.alwaysBounceVertical = true
        scrollView.delegate = self
        view.addSubview(scrollView)
        
        // 头像view的背景
        let viewBG = UIView(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: 128))
        viewBG.backgroundColor = Fatcat.THEME_COLOR
        scrollView.addSubview(viewBG)
        
        //MARK: 头像 默认加载一个缺省图片
        avatar = UIButton(frame: CGRect(x: 0, y: 16, width: 72, height: 72))
        avatar.center.x = view.center.x
        //avatar.backgroundColor = UIColor.lightGrayColor()
        avatar.layer.borderWidth = 2
        avatar.layer.borderColor = UIColor.whiteColor().CGColor
        avatar.layer.cornerRadius = 36
        avatar.layer.masksToBounds = true
        viewBG.addSubview(avatar)
        avatar.setImage(UIImage(named: "avatar_default_m"), forState: .Normal)
        
        // MARK:userBtn 未登录显示登陆，已登录显示用户名
        userBtn = UIButton(frame: CGRect(x: 0, y: 96, width: view.bounds.width - 32, height: 24))
        userBtn.center.x = view.center.x
        userBtn.setTitle("登陆", forState: .Normal)
        userBtn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        userBtn.titleLabel?.font = UIFont.systemFontOfSize(14)
        viewBG.addSubview(userBtn)
        
        avatar.addTarget(self, action: #selector(MemberViewController.gotoUser), forControlEvents: .TouchUpInside)
        userBtn.addTarget(self, action: #selector(MemberViewController.gotoUser), forControlEvents: .TouchUpInside)
        
        // MARK:tableview
        tableView = UITableView(frame: CGRect(x: 0, y: 128, width: view.bounds.width, height: view.bounds.height - 128), style: .Grouped)
        scrollView.addSubview(tableView)
        tableView.delegate = self
        tableView.dataSource = self
        scrollView.contentSize = CGSize(width: view.bounds.width, height: tableView.frame.height + 148)
        
        // MARK:是否登陆
        if NSUserDefaults.standardUserDefaults().objectForKey("user") != nil{
            logined = true
            userBtn.setTitle(NSUserDefaults.standardUserDefaults().objectForKey("user") as? String, forState: .Normal)
        }
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        //navigationController?.setNavigationBarHidden(true, animated: false)
        navigationController?.navigationBar.hidden = true
        
        if NSUserDefaults.standardUserDefaults().objectForKey("user") != nil {
            logined = true
            userBtn.setTitle(NSUserDefaults.standardUserDefaults().objectForKey("user") as? String, forState: .Normal)
        }else{
            logined = false
            userBtn.setTitle("登陆", forState: .Normal)
        }
        
    }
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        if scrollView.contentOffset.y > 20 {
            view.backgroundColor = UIColor(red: 239.0/255.0, green: 239.0/255.0, blue: 244.0/255.0, alpha: 1)
        }else{
            view.backgroundColor = Fatcat.THEME_COLOR
        }
    }
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return TABLE_ITEMS.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .Default, reuseIdentifier: "item")
        cell.accessoryType = .DisclosureIndicator
        let icon = UIImageView(frame: CGRect(x: 16, y: 8, width: 28, height: 28))
        
        cell.addSubview(icon)
        
        switch indexPath.row {
        case 0:
            icon.image = UIImage(named: "profile")
            break
        case 1:
            icon.image = UIImage(named: "documents_icon_folder")
            break
//        case 2:
//            icon.image = UIImage(named: "address")
//            break
        case 2:
            icon.image = UIImage(named: "card")
            break
        case 3:
            icon.image = UIImage(named: "setting")
            break
        case 4:
            icon.image = UIImage(named: "help")
            break
        default:
            icon.backgroundColor = UIColor.grayColor()
            break
        }
        
        let textLabel = UILabel(frame: CGRect(x: 16+28+8, y: 8, width: view.bounds.width - 32 - 28 - 8, height: 28))
        cell.addSubview(textLabel)
        
        textLabel.text = TABLE_ITEMS[indexPath.row]
        return cell
    }
    
    func gotoUser(){
        if logined{
            hidesBottomBarWhenPushed = true
            navigationController?.navigationBar.hidden = false
            navigationController?.pushViewController(UserViewController(), animated: true)
        }else{
            presentViewController(UDLoginViewController(), animated: true, completion: nil)
        }
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        if indexPath.section == 0{
            switch indexPath.row {
            case 0:
                gotoUser()
                break
            case 1:
                if logined{
                    hidesBottomBarWhenPushed = true
                    navigationController?.navigationBar.hidden = false
                    navigationController?.pushViewController(CloudFileViewController(), animated: true)
                }else{
                    presentViewController(UDLoginViewController(), animated: true, completion: nil)
                }
                break
//            case 2:
//                hidesBottomBarWhenPushed = true
//                navigationController?.navigationBar.hidden = false
//                navigationController?.pushViewController(AdressManageViewController(managerType: .Edit), animated: true)
//                
//                break
            case 4:
                hidesBottomBarWhenPushed = true
                navigationController?.navigationBar.hidden = false
                navigationController?.pushViewController(HelpViewController(), animated: true)
                break
            default:
                break
            }
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        hidesBottomBarWhenPushed = false
        
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
